const express = require('express');

const fs = require('fs');
const app = express();
const port = 3000;

const https = require('https');

app.use(express.static(__dirname, {
  index: 'signIn.html'
}));

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get('/getQuestions', (req, res) => {
  let response = {};
  response['1'] = '你好吗';
  response['2'] = '如何';
  response['3'] = '打算';
  response['4'] = '身体是否异常';
  response['4-1'] = '是';
  response['4-2'] = '否';
  response['4-1-1'] = '最近去过哪里';
  response['4-1-2'] = '体温';
  res.json(response);
});

app.get('/getAnswers', (req, res) => {
  let response = {};
  response['1'] = '不好';
  response['2'] = '还行';
  response['3'] = '放假';
  response['4'] = '4-2';
  response.isToday = false;
  // response['4-1-1'] = '东莞'; 
  // response['4-1-2'] = '37.8'; 

  res.json(response);
});


app.post('/submitFormData', (req, res) => {
  let response = {};
  response.message = 'Submit successfully';
  res.json(response);
});

app.post('/queryUsers', (req, res) => {

  let stream = fs.createReadStream(`${__dirname}/testData/queryData.json`);
  stream.on('close', () => {
    console.log('close queryData.json');
  })
  stream.on('readable', () => {
    let queryData = JSON.parse(stream.read().toString());
    res.json(queryData);
    stream.close();
  })

});

app.get('/initCharts', (req, res) => {

  let stream = fs.createReadStream(`${__dirname}/testData/chartData.json`);
  let re = /\s/g;
  stream.on('close', () => {
    console.log('close chartData.json');
  })
  stream.on('readable', () => {
    let chartData = JSON.parse(stream.read().toString().replace(re, ''));
    res.json(chartData);
    stream.close();
  })

});

app.get('/getUsers', (req, res) => {

  let stream = fs.createReadStream(`${__dirname}/testData/usersData.json`);
  let re = /\s/g;
  stream.on('close', () => {
    console.log('close usersData.json');
  })
  stream.on('readable', () => {
    let usersData = JSON.parse(stream.read().toString().replace(re, ''));
    res.json(usersData);
    stream.close();
  })

});

app.get('/getAdmin', (req, res) => {

  let stream = fs.createReadStream(`${__dirname}/testData/adminData.json`);
  let re = /\s/g;
  stream.on('close', () => {
    console.log('close adminData.json');
  })
  stream.on('readable', () => {
    let adminData = JSON.parse(stream.read().toString().replace(re, ''));
    res.json(adminData);
    stream.close();
  })

});

app.post('/check007', (req, res) => {
  const params = new URLSearchParams({
    Action: 'DescribeCaptchaResult',
    Version: '2019-07-22',
    CaptchaType: 9,
    Ticket: req.body.ticket,
    UserIp: '127.0.0.1',
    Randstr: req.body.randstr,
    CaptchaAppId: '2054640852',
    AppSecretKey: '0mq0gGKio0v7tNy-RSzHlCQ**'
  });
  let url = new URL('https://captcha.tencentcloudapi.com/');
  url.search = params.toString();
  
  https.request(url, {
    port: 80
  },
  (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);
  
    res.on('data', (d) => {
      process.stdout.write(d);
    });
  }
  );

  res.json({x: 'success'});
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});






